{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 单只下载"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "\n",
    "xtdata.download_history_data(stock_code='002358.SZ', period='1d', start_time='20230101', end_time='20231115')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 批量下载"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'finished': 0, 'total': 1, 'stockcode': '开始下载', 'message': ''}\n",
      "{'finished': 0, 'total': 3, 'stockcode': '', 'message': ''}\n",
      "{'finished': 3, 'total': 3, 'stockcode': '002714.SZ', 'message': ''}\n"
     ]
    }
   ],
   "source": [
    "from xtquant import xtdata\n",
    "\n",
    "def on_progress(data):\n",
    "        print(data)\n",
    "\n",
    "xtdata.download_history_data2(stock_list=['600309.SH', '600989.SH', '002714.SZ'], period='tick', start_time='20231111', end_time='20231115', callback=on_progress)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 获取历史数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "           20231113  20231114  20231115\n",
      "600309.SH     83.48     83.26     83.18\n",
      "600989.SH     14.29     14.29     14.40\n",
      "002714.SZ     37.52     37.45     37.47\n"
     ]
    }
   ],
   "source": [
    "from xtquant import xtdata\n",
    "\n",
    "xtdata.download_history_data2(stock_list=['600309.SH', '600989.SH', '002714.SZ'], period='1d', start_time='20231111', end_time='20231115')\n",
    "\n",
    "res = xtdata.get_market_data(stock_list=['600309.SH', '600989.SH', '002714.SZ'], period='1d', start_time='20231111', end_time='20231115')\n",
    "\n",
    "print(res['open'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "***** xtdata连接成功 *****\n",
      "服务信息: {'tag': 'xtquant', 'version': '1.0'}\n",
      "服务地址: 127.0.0.1:58609\n",
      "数据路径: d:\\Code\\course\\miniqmt-demo\\data\\datadir\n",
      "设置xtdata.enable_hello = False可隐藏此消息\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>amount</th>\n",
       "      <th>settelementPrice</th>\n",
       "      <th>openInterest</th>\n",
       "      <th>preClose</th>\n",
       "      <th>suspendFlag</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>20231113</th>\n",
       "      <td>1699804800000</td>\n",
       "      <td>83.48</td>\n",
       "      <td>83.72</td>\n",
       "      <td>82.52</td>\n",
       "      <td>83.10</td>\n",
       "      <td>92356</td>\n",
       "      <td>766722805.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>15</td>\n",
       "      <td>83.33</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20231114</th>\n",
       "      <td>1699891200000</td>\n",
       "      <td>83.26</td>\n",
       "      <td>83.98</td>\n",
       "      <td>82.01</td>\n",
       "      <td>82.68</td>\n",
       "      <td>101943</td>\n",
       "      <td>841974077.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>15</td>\n",
       "      <td>83.10</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20231115</th>\n",
       "      <td>1699977600000</td>\n",
       "      <td>83.18</td>\n",
       "      <td>83.83</td>\n",
       "      <td>82.47</td>\n",
       "      <td>83.30</td>\n",
       "      <td>105194</td>\n",
       "      <td>873683395.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>15</td>\n",
       "      <td>82.68</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   time   open   high    low  close  volume       amount  \\\n",
       "20231113  1699804800000  83.48  83.72  82.52  83.10   92356  766722805.0   \n",
       "20231114  1699891200000  83.26  83.98  82.01  82.68  101943  841974077.0   \n",
       "20231115  1699977600000  83.18  83.83  82.47  83.30  105194  873683395.0   \n",
       "\n",
       "          settelementPrice  openInterest  preClose  suspendFlag  \n",
       "20231113               0.0            15     83.33            0  \n",
       "20231114               0.0            15     83.10            0  \n",
       "20231115               0.0            15     82.68            0  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from xtquant import xtdata\n",
    "\n",
    "xtdata.connect(port=58609)\n",
    "\n",
    "xtdata.download_history_data2(stock_list=['600309.SH', '600989.SH', '002714.SZ'], period='1d', start_time='20231111', end_time='20231115')\n",
    "\n",
    "res = xtdata.get_local_data(stock_list=['600309.SH', '600989.SH', '002714.SZ'], period='1d', start_time='20231111', end_time='20231115')\n",
    "\n",
    "res['600309.SH']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 周期"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time</th>\n",
       "      <th>lastPrice</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>lastClose</th>\n",
       "      <th>amount</th>\n",
       "      <th>volume</th>\n",
       "      <th>pvolume</th>\n",
       "      <th>stockStatus</th>\n",
       "      <th>openInt</th>\n",
       "      <th>lastSettlementPrice</th>\n",
       "      <th>askPrice</th>\n",
       "      <th>bidPrice</th>\n",
       "      <th>askVol</th>\n",
       "      <th>bidVol</th>\n",
       "      <th>settlementPrice</th>\n",
       "      <th>transactionNum</th>\n",
       "      <th>pe</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>20231113091501</th>\n",
       "      <td>1699838101000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>83.33</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>[0.0, 0.0, 0.0, 0.0, 0.0]</td>\n",
       "      <td>[0.0, 0.0, 0.0, 0.0, 0.0]</td>\n",
       "      <td>[0, 0, 0, 0, 0]</td>\n",
       "      <td>[0, 0, 0, 0, 0]</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20231113091504</th>\n",
       "      <td>1699838104000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>83.33</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>[83.34, 0.0, 0.0, 0.0, 0.0]</td>\n",
       "      <td>[83.34, 0.0, 0.0, 0.0, 0.0]</td>\n",
       "      <td>[5, 0, 0, 0, 0]</td>\n",
       "      <td>[5, 0, 0, 0, 0]</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20231113091507</th>\n",
       "      <td>1699838107000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>83.33</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>[83.34, 0.0, 0.0, 0.0, 0.0]</td>\n",
       "      <td>[83.34, 0.0, 0.0, 0.0, 0.0]</td>\n",
       "      <td>[5, 0, 0, 0, 0]</td>\n",
       "      <td>[5, 13, 0, 0, 0]</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20231113091510</th>\n",
       "      <td>1699838110000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>83.33</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>[83.34, 0.0, 0.0, 0.0, 0.0]</td>\n",
       "      <td>[83.34, 0.0, 0.0, 0.0, 0.0]</td>\n",
       "      <td>[12, 0, 0, 0, 0]</td>\n",
       "      <td>[12, 8, 0, 0, 0]</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20231113091513</th>\n",
       "      <td>1699838113000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>83.33</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>[83.34, 0.0, 0.0, 0.0, 0.0]</td>\n",
       "      <td>[83.34, 0.0, 0.0, 0.0, 0.0]</td>\n",
       "      <td>[17, 0, 0, 0, 0]</td>\n",
       "      <td>[17, 15, 0, 0, 0]</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20231115145951</th>\n",
       "      <td>1700031591000</td>\n",
       "      <td>83.29</td>\n",
       "      <td>83.18</td>\n",
       "      <td>83.83</td>\n",
       "      <td>82.47</td>\n",
       "      <td>82.68</td>\n",
       "      <td>862712785.0</td>\n",
       "      <td>103877</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>18</td>\n",
       "      <td>0.0</td>\n",
       "      <td>[83.3, 0.0, 0.0, 0.0, 0.0]</td>\n",
       "      <td>[83.3, 0.0, 0.0, 0.0, 0.0]</td>\n",
       "      <td>[1130, 0, 0, 0, 0]</td>\n",
       "      <td>[1130, 186, 0, 0, 0]</td>\n",
       "      <td>0.0</td>\n",
       "      <td>42445</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20231115145954</th>\n",
       "      <td>1700031594000</td>\n",
       "      <td>83.29</td>\n",
       "      <td>83.18</td>\n",
       "      <td>83.83</td>\n",
       "      <td>82.47</td>\n",
       "      <td>82.68</td>\n",
       "      <td>862712785.0</td>\n",
       "      <td>103877</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>18</td>\n",
       "      <td>0.0</td>\n",
       "      <td>[83.3, 0.0, 0.0, 0.0, 0.0]</td>\n",
       "      <td>[83.3, 0.0, 0.0, 0.0, 0.0]</td>\n",
       "      <td>[1199, 0, 0, 0, 0]</td>\n",
       "      <td>[1199, 117, 0, 0, 0]</td>\n",
       "      <td>0.0</td>\n",
       "      <td>42445</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20231115145957</th>\n",
       "      <td>1700031597000</td>\n",
       "      <td>83.29</td>\n",
       "      <td>83.18</td>\n",
       "      <td>83.83</td>\n",
       "      <td>82.47</td>\n",
       "      <td>82.68</td>\n",
       "      <td>862712785.0</td>\n",
       "      <td>103877</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>18</td>\n",
       "      <td>0.0</td>\n",
       "      <td>[83.3, 0.0, 0.0, 0.0, 0.0]</td>\n",
       "      <td>[83.3, 0.0, 0.0, 0.0, 0.0]</td>\n",
       "      <td>[1316, 58, 0, 0, 0]</td>\n",
       "      <td>[1316, 0, 0, 0, 0]</td>\n",
       "      <td>0.0</td>\n",
       "      <td>42445</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20231115150000</th>\n",
       "      <td>1700031600000</td>\n",
       "      <td>83.29</td>\n",
       "      <td>83.18</td>\n",
       "      <td>83.83</td>\n",
       "      <td>82.47</td>\n",
       "      <td>82.68</td>\n",
       "      <td>862712785.0</td>\n",
       "      <td>103877</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>18</td>\n",
       "      <td>0.0</td>\n",
       "      <td>[83.3, 0.0, 0.0, 0.0, 0.0]</td>\n",
       "      <td>[83.3, 0.0, 0.0, 0.0, 0.0]</td>\n",
       "      <td>[1317, 143, 0, 0, 0]</td>\n",
       "      <td>[1317, 0, 0, 0, 0]</td>\n",
       "      <td>0.0</td>\n",
       "      <td>42445</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20231115150003</th>\n",
       "      <td>1700031603000</td>\n",
       "      <td>83.30</td>\n",
       "      <td>83.18</td>\n",
       "      <td>83.83</td>\n",
       "      <td>82.47</td>\n",
       "      <td>82.68</td>\n",
       "      <td>873683395.0</td>\n",
       "      <td>105194</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>15</td>\n",
       "      <td>0.0</td>\n",
       "      <td>[83.3, 83.31, 83.32, 83.33, 83.34]</td>\n",
       "      <td>[83.29, 83.28, 83.27, 83.26, 83.25]</td>\n",
       "      <td>[143, 19, 4, 7, 6]</td>\n",
       "      <td>[38, 31, 49, 65, 20]</td>\n",
       "      <td>0.0</td>\n",
       "      <td>42657</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>14944 rows × 19 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                         time  lastPrice   open   high    low  lastClose  \\\n",
       "20231113091501  1699838101000       0.00   0.00   0.00   0.00      83.33   \n",
       "20231113091504  1699838104000       0.00   0.00   0.00   0.00      83.33   \n",
       "20231113091507  1699838107000       0.00   0.00   0.00   0.00      83.33   \n",
       "20231113091510  1699838110000       0.00   0.00   0.00   0.00      83.33   \n",
       "20231113091513  1699838113000       0.00   0.00   0.00   0.00      83.33   \n",
       "...                       ...        ...    ...    ...    ...        ...   \n",
       "20231115145951  1700031591000      83.29  83.18  83.83  82.47      82.68   \n",
       "20231115145954  1700031594000      83.29  83.18  83.83  82.47      82.68   \n",
       "20231115145957  1700031597000      83.29  83.18  83.83  82.47      82.68   \n",
       "20231115150000  1700031600000      83.29  83.18  83.83  82.47      82.68   \n",
       "20231115150003  1700031603000      83.30  83.18  83.83  82.47      82.68   \n",
       "\n",
       "                     amount  volume  pvolume  stockStatus  openInt  \\\n",
       "20231113091501          0.0       0        0            0       12   \n",
       "20231113091504          0.0       0        0            0       12   \n",
       "20231113091507          0.0       0        0            0       12   \n",
       "20231113091510          0.0       0        0            0       12   \n",
       "20231113091513          0.0       0        0            0       12   \n",
       "...                     ...     ...      ...          ...      ...   \n",
       "20231115145951  862712785.0  103877        0            0       18   \n",
       "20231115145954  862712785.0  103877        0            0       18   \n",
       "20231115145957  862712785.0  103877        0            0       18   \n",
       "20231115150000  862712785.0  103877        0            0       18   \n",
       "20231115150003  873683395.0  105194        0            0       15   \n",
       "\n",
       "                lastSettlementPrice                            askPrice  \\\n",
       "20231113091501                  0.0           [0.0, 0.0, 0.0, 0.0, 0.0]   \n",
       "20231113091504                  0.0         [83.34, 0.0, 0.0, 0.0, 0.0]   \n",
       "20231113091507                  0.0         [83.34, 0.0, 0.0, 0.0, 0.0]   \n",
       "20231113091510                  0.0         [83.34, 0.0, 0.0, 0.0, 0.0]   \n",
       "20231113091513                  0.0         [83.34, 0.0, 0.0, 0.0, 0.0]   \n",
       "...                             ...                                 ...   \n",
       "20231115145951                  0.0          [83.3, 0.0, 0.0, 0.0, 0.0]   \n",
       "20231115145954                  0.0          [83.3, 0.0, 0.0, 0.0, 0.0]   \n",
       "20231115145957                  0.0          [83.3, 0.0, 0.0, 0.0, 0.0]   \n",
       "20231115150000                  0.0          [83.3, 0.0, 0.0, 0.0, 0.0]   \n",
       "20231115150003                  0.0  [83.3, 83.31, 83.32, 83.33, 83.34]   \n",
       "\n",
       "                                           bidPrice                askVol  \\\n",
       "20231113091501            [0.0, 0.0, 0.0, 0.0, 0.0]       [0, 0, 0, 0, 0]   \n",
       "20231113091504          [83.34, 0.0, 0.0, 0.0, 0.0]       [5, 0, 0, 0, 0]   \n",
       "20231113091507          [83.34, 0.0, 0.0, 0.0, 0.0]       [5, 0, 0, 0, 0]   \n",
       "20231113091510          [83.34, 0.0, 0.0, 0.0, 0.0]      [12, 0, 0, 0, 0]   \n",
       "20231113091513          [83.34, 0.0, 0.0, 0.0, 0.0]      [17, 0, 0, 0, 0]   \n",
       "...                                             ...                   ...   \n",
       "20231115145951           [83.3, 0.0, 0.0, 0.0, 0.0]    [1130, 0, 0, 0, 0]   \n",
       "20231115145954           [83.3, 0.0, 0.0, 0.0, 0.0]    [1199, 0, 0, 0, 0]   \n",
       "20231115145957           [83.3, 0.0, 0.0, 0.0, 0.0]   [1316, 58, 0, 0, 0]   \n",
       "20231115150000           [83.3, 0.0, 0.0, 0.0, 0.0]  [1317, 143, 0, 0, 0]   \n",
       "20231115150003  [83.29, 83.28, 83.27, 83.26, 83.25]    [143, 19, 4, 7, 6]   \n",
       "\n",
       "                              bidVol  settlementPrice  transactionNum   pe  \n",
       "20231113091501       [0, 0, 0, 0, 0]              0.0               0  0.0  \n",
       "20231113091504       [5, 0, 0, 0, 0]              0.0               0  0.0  \n",
       "20231113091507      [5, 13, 0, 0, 0]              0.0               0  0.0  \n",
       "20231113091510      [12, 8, 0, 0, 0]              0.0               0  0.0  \n",
       "20231113091513     [17, 15, 0, 0, 0]              0.0               0  0.0  \n",
       "...                              ...              ...             ...  ...  \n",
       "20231115145951  [1130, 186, 0, 0, 0]              0.0           42445  0.0  \n",
       "20231115145954  [1199, 117, 0, 0, 0]              0.0           42445  0.0  \n",
       "20231115145957    [1316, 0, 0, 0, 0]              0.0           42445  0.0  \n",
       "20231115150000    [1317, 0, 0, 0, 0]              0.0           42445  0.0  \n",
       "20231115150003  [38, 31, 49, 65, 20]              0.0           42657  0.0  \n",
       "\n",
       "[14944 rows x 19 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from xtquant import xtdata\n",
    "\n",
    "xtdata.download_history_data2(stock_list=['600309.SH'], period='tick', start_time='20231111', end_time='20231115')\n",
    "\n",
    "res = xtdata.get_local_data(stock_list=['600309.SH'], period='tick', start_time='20231111', end_time='20231115')\n",
    "\n",
    "res['600309.SH']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 【VIP】获取可用周期"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "***** xtdata连接成功 *****\n",
      "服务信息: {'tag': 'xtquant', 'version': '1.0'}\n",
      "服务地址: 127.0.0.1:58609\n",
      "数据路径: d:\\Code\\course\\miniqmt-demo\\data\\datadir\n",
      "设置xtdata.enable_hello = False可隐藏此消息\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['10m',\n",
       " '15m',\n",
       " '1d',\n",
       " '1h',\n",
       " '1hy',\n",
       " '1m',\n",
       " '1mon',\n",
       " '1q',\n",
       " '1w',\n",
       " '1y',\n",
       " '2d',\n",
       " '2h',\n",
       " '30m',\n",
       " '3d',\n",
       " '3h',\n",
       " '3m',\n",
       " '4h',\n",
       " '5d',\n",
       " '5m',\n",
       " '60m',\n",
       " 'balance',\n",
       " 'capital',\n",
       " 'cashflow',\n",
       " 'delistchangebond',\n",
       " 'dividendplaninfo',\n",
       " 'futureholderrank',\n",
       " 'historycontract',\n",
       " 'historymaincontract',\n",
       " 'hkbrokerqueue',\n",
       " 'holdernum',\n",
       " 'income',\n",
       " 'interactiveqa',\n",
       " 'l2order',\n",
       " 'l2orderqueue',\n",
       " 'l2quote',\n",
       " 'l2quoteaux',\n",
       " 'l2thousand',\n",
       " 'l2transaction',\n",
       " 'l2transactioncount',\n",
       " 'northfinancechange1d',\n",
       " 'northfinancechange1m',\n",
       " 'optionhistorycontract',\n",
       " 'orderflow15m',\n",
       " 'orderflow1d',\n",
       " 'orderflow1h',\n",
       " 'orderflow1m',\n",
       " 'orderflow30m',\n",
       " 'orderflow5m',\n",
       " 'pershareindex',\n",
       " 'replacechangebond',\n",
       " 'snapshotindex',\n",
       " 'specialtreatment',\n",
       " 'tick',\n",
       " 'top10flowholder',\n",
       " 'top10holder',\n",
       " 'transactioncount1d',\n",
       " 'transactioncount1m',\n",
       " 'warehousereceipt']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from xtquant import xtdata\n",
    "\n",
    "xtdata.reconnect(port=58609)\n",
    "\n",
    "res = xtdata.get_period_list()\n",
    "\n",
    "res"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 复权"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>amount</th>\n",
       "      <th>settelementPrice</th>\n",
       "      <th>openInterest</th>\n",
       "      <th>preClose</th>\n",
       "      <th>suspendFlag</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>20231113</th>\n",
       "      <td>1699804800000</td>\n",
       "      <td>2909.107885</td>\n",
       "      <td>2917.471396</td>\n",
       "      <td>2875.653841</td>\n",
       "      <td>2895.865659</td>\n",
       "      <td>92356</td>\n",
       "      <td>766722805.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>15</td>\n",
       "      <td>2903.880690</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20231114</th>\n",
       "      <td>1699891200000</td>\n",
       "      <td>2901.441333</td>\n",
       "      <td>2926.531866</td>\n",
       "      <td>2857.881380</td>\n",
       "      <td>2881.229515</td>\n",
       "      <td>101943</td>\n",
       "      <td>841974077.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>15</td>\n",
       "      <td>2895.865659</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20231115</th>\n",
       "      <td>1699977600000</td>\n",
       "      <td>2898.653496</td>\n",
       "      <td>2921.304672</td>\n",
       "      <td>2873.911443</td>\n",
       "      <td>2902.835252</td>\n",
       "      <td>105194</td>\n",
       "      <td>873683395.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>15</td>\n",
       "      <td>2881.229515</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   time         open         high          low        close  \\\n",
       "20231113  1699804800000  2909.107885  2917.471396  2875.653841  2895.865659   \n",
       "20231114  1699891200000  2901.441333  2926.531866  2857.881380  2881.229515   \n",
       "20231115  1699977600000  2898.653496  2921.304672  2873.911443  2902.835252   \n",
       "\n",
       "          volume       amount  settelementPrice  openInterest     preClose  \\\n",
       "20231113   92356  766722805.0               0.0            15  2903.880690   \n",
       "20231114  101943  841974077.0               0.0            15  2895.865659   \n",
       "20231115  105194  873683395.0               0.0            15  2881.229515   \n",
       "\n",
       "          suspendFlag  \n",
       "20231113            0  \n",
       "20231114            0  \n",
       "20231115            0  "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from xtquant import xtdata\n",
    "\n",
    "# 不复权\n",
    "# res = xtdata.get_local_data(stock_list=['600309.SH'], period='1d', start_time='20231111', end_time='20231115')\n",
    "\n",
    "# 前复权\n",
    "res = xtdata.get_local_data(stock_list=['600309.SH'], period='1d', start_time='20231111', end_time='20231115', dividend_type='front_ratio')\n",
    "\n",
    "# 后复权\n",
    "res = xtdata.get_local_data(stock_list=['600309.SH'], period='1d', start_time='20231111', end_time='20231115', dividend_type='back_ratio')\n",
    "\n",
    "res['600309.SH']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 分红(除权)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time</th>\n",
       "      <th>interest</th>\n",
       "      <th>stockBonus</th>\n",
       "      <th>stockGift</th>\n",
       "      <th>allotNum</th>\n",
       "      <th>allotPrice</th>\n",
       "      <th>gugai</th>\n",
       "      <th>dr</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>20010419</th>\n",
       "      <td>9.876096e+11</td>\n",
       "      <td>0.10</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.8</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.003194</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20020415</th>\n",
       "      <td>1.018800e+12</td>\n",
       "      <td>0.20</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.613157</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20030611</th>\n",
       "      <td>1.055261e+12</td>\n",
       "      <td>0.20</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.016666</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20040517</th>\n",
       "      <td>1.084723e+12</td>\n",
       "      <td>0.20</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.719758</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20050401</th>\n",
       "      <td>1.112285e+12</td>\n",
       "      <td>0.20</td>\n",
       "      <td>0.3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.314977</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20060605</th>\n",
       "      <td>1.149437e+12</td>\n",
       "      <td>0.15</td>\n",
       "      <td>0.4</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.409700</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20070711</th>\n",
       "      <td>1.184083e+12</td>\n",
       "      <td>0.10</td>\n",
       "      <td>0.4</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.402735</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20080414</th>\n",
       "      <td>1.208102e+12</td>\n",
       "      <td>0.40</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.013377</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20090414</th>\n",
       "      <td>1.239638e+12</td>\n",
       "      <td>0.50</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.027548</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20100517</th>\n",
       "      <td>1.274026e+12</td>\n",
       "      <td>0.30</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.015898</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20110512</th>\n",
       "      <td>1.305130e+12</td>\n",
       "      <td>0.40</td>\n",
       "      <td>0.3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.321978</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20120424</th>\n",
       "      <td>1.335197e+12</td>\n",
       "      <td>0.60</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.043572</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20130503</th>\n",
       "      <td>1.367510e+12</td>\n",
       "      <td>0.70</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.038588</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20140616</th>\n",
       "      <td>1.402848e+12</td>\n",
       "      <td>0.70</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.043859</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20150528</th>\n",
       "      <td>1.432742e+12</td>\n",
       "      <td>0.30</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.010522</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20160422</th>\n",
       "      <td>1.461254e+12</td>\n",
       "      <td>0.20</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.012070</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170518</th>\n",
       "      <td>1.495037e+12</td>\n",
       "      <td>0.50</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.221649</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20180126</th>\n",
       "      <td>1.516896e+12</td>\n",
       "      <td>1.50</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.041163</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20190527</th>\n",
       "      <td>1.558886e+12</td>\n",
       "      <td>2.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.052438</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20200515</th>\n",
       "      <td>1.589472e+12</td>\n",
       "      <td>1.30</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.029076</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20210426</th>\n",
       "      <td>1.619366e+12</td>\n",
       "      <td>1.30</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.011837</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20220429</th>\n",
       "      <td>1.651162e+12</td>\n",
       "      <td>2.50</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.032270</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20230526</th>\n",
       "      <td>1.685030e+12</td>\n",
       "      <td>1.60</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.018796</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  time  interest  stockBonus  stockGift  allotNum  allotPrice  \\\n",
       "20010419  9.876096e+11      0.10         0.2        0.8       0.0         0.0   \n",
       "20020415  1.018800e+12      0.20         0.1        0.5       0.0         0.0   \n",
       "20030611  1.055261e+12      0.20         0.0        0.0       0.0         0.0   \n",
       "20040517  1.084723e+12      0.20         0.2        0.5       0.0         0.0   \n",
       "20050401  1.112285e+12      0.20         0.3        0.0       0.0         0.0   \n",
       "20060605  1.149437e+12      0.15         0.4        0.0       0.0         0.0   \n",
       "20070711  1.184083e+12      0.10         0.4        0.0       0.0         0.0   \n",
       "20080414  1.208102e+12      0.40         0.0        0.0       0.0         0.0   \n",
       "20090414  1.239638e+12      0.50         0.0        0.0       0.0         0.0   \n",
       "20100517  1.274026e+12      0.30         0.0        0.0       0.0         0.0   \n",
       "20110512  1.305130e+12      0.40         0.3        0.0       0.0         0.0   \n",
       "20120424  1.335197e+12      0.60         0.0        0.0       0.0         0.0   \n",
       "20130503  1.367510e+12      0.70         0.0        0.0       0.0         0.0   \n",
       "20140616  1.402848e+12      0.70         0.0        0.0       0.0         0.0   \n",
       "20150528  1.432742e+12      0.30         0.0        0.0       0.0         0.0   \n",
       "20160422  1.461254e+12      0.20         0.0        0.0       0.0         0.0   \n",
       "20170518  1.495037e+12      0.50         0.2        0.0       0.0         0.0   \n",
       "20180126  1.516896e+12      1.50         0.0        0.0       0.0         0.0   \n",
       "20190527  1.558886e+12      2.00         0.0        0.0       0.0         0.0   \n",
       "20200515  1.589472e+12      1.30         0.0        0.0       0.0         0.0   \n",
       "20210426  1.619366e+12      1.30         0.0        0.0       0.0         0.0   \n",
       "20220429  1.651162e+12      2.50         0.0        0.0       0.0         0.0   \n",
       "20230526  1.685030e+12      1.60         0.0        0.0       0.0         0.0   \n",
       "\n",
       "          gugai        dr  \n",
       "20010419    0.0  2.003194  \n",
       "20020415    0.0  1.613157  \n",
       "20030611    0.0  1.016666  \n",
       "20040517    0.0  1.719758  \n",
       "20050401    0.0  1.314977  \n",
       "20060605    0.0  1.409700  \n",
       "20070711    0.0  1.402735  \n",
       "20080414    0.0  1.013377  \n",
       "20090414    0.0  1.027548  \n",
       "20100517    0.0  1.015898  \n",
       "20110512    0.0  1.321978  \n",
       "20120424    0.0  1.043572  \n",
       "20130503    0.0  1.038588  \n",
       "20140616    0.0  1.043859  \n",
       "20150528    0.0  1.010522  \n",
       "20160422    0.0  1.012070  \n",
       "20170518    0.0  1.221649  \n",
       "20180126    0.0  1.041163  \n",
       "20190527    0.0  1.052438  \n",
       "20200515    0.0  1.029076  \n",
       "20210426    0.0  1.011837  \n",
       "20220429    0.0  1.032270  \n",
       "20230526    0.0  1.018796  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from xtquant import xtdata\n",
    "\n",
    "res = xtdata.get_divid_factors(stock_code='600309.SH')\n",
    "\n",
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'000001.SZ':                    time   open   high    low  close  volume        amount  \\\n",
      "20240403  1712073600000  10.53  10.55  10.42  10.46  981846  1.028649e+09   \n",
      "\n",
      "          settelementPrice  openInterest  preClose  suspendFlag  \n",
      "20240403               0.0            15     10.55            0  }\n"
     ]
    }
   ],
   "source": [
    "from xtquant import xtdata\n",
    "\n",
    "xtdata.connect(port=58609)\n",
    "stock = '000001.SZ'\n",
    "# field = 'lastPrice'\n",
    "xtdata.download_history_data(period='1d',stock_code=stock,start_time='20240401',end_time='20240405')\n",
    "data = xtdata.get_market_data_ex(stock_list=[stock], period='1d',start_time='20240403')\n",
    "# detail = xtdata.get_instrument_detail(stock)\n",
    "# 总市值 = 最新价 * 总股本\n",
    "# price = data[stock].iloc[-1][field] * detail['TotalVolume']\n",
    "print(data)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "miniqmt-demo-cptEsqYo-py3.10",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
